<!-- warehouseDetail -->
<template>
  <el-card>
    <el-form :model="formData" :rules="formRules" :inline="true" label-position="top">
      <el-form-item label="仓库编码" prop="code">
        <el-input v-model="formData.code" :disabled="true" style="width: 300px" />
      </el-form-item>
      <el-form-item label="仓库名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入" style="width: 300px" />
      </el-form-item>
      <el-form-item label="仓库类型" prop="type">
        <el-select v-model="formData.type" placeholder="请选择" style="width: 300px">
          <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="省/市/区" prop="location" style="width: 800px">
        <!-- <el-input placeholder="请选择">111</el-input> -->
        <el-cascader
          v-model="selectedOptions"
          :options="options"
          clearable
          class="PVC"
          size="large"
          style="width: 100%"
          @change="changeFn"
        />
      </el-form-item>
      <el-form-item label="详细地址" prop="address" style="width: 800px">
        <el-input v-model="formData.address" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="仓库状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio :label="0">启用</el-radio>
          <el-radio :label="1">停用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="仓库面积" prop="surface">
        <el-input v-model="formData.surface" style="width: 300px;" placeholder="请输入">
          <template slot="append">m²</template>
        </el-input>
      </el-form-item>
      <el-form-item label="负责人" prop="personName">
        <el-input v-model="formData.personName" placeholder="请输入" style="width: 300px" />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="formData.phone" placeholder="请输入" style="width: 300px" />
      </el-form-item>
    </el-form>
    <div class="addFooter">
      <el-button round @click="$router.go(-1)">返回</el-button>
      <el-button round type="primary" @click="submit">提交</el-button>
    </div>
  </el-card>
</template>

<script>
import { updateList, aupdataCK } from '@/api'
import { regionData, CodeToText, TextToCode } from 'element-china-area-data'
export default {
  data() {
    return {
      options: regionData, // 使用中国省市区数组
      selectedOptions: [], // 店铺地址数组
      formData: {
        code: '', // 仓库编码
        name: '', // 仓库名称
        type: '', // 仓库类型
        location: '', // 省/市/区
        address: '', // 详细地址
        status: 0, // 仓库状态
        surface: '', // 仓库面积
        personName: '', // 负责人
        phone: '' // 联系电话
      },
      typeList: [
        {
          id: 'ZZ',
          name: '中转仓'
        },
        {
          id: 'JG',
          name: '加工仓'
        },
        {
          id: 'CB',
          name: '储备仓'
        },
        {
          id: 'LC',
          name: '冷藏仓'
        }
      ],
      formRules: {
        name: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        location: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        personName: [
          { required: true, message: '请输入', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    const id = JSON.parse(localStorage.getItem('CKID'))
    const res = await updateList(id)
    // console.log(res)
    this.formData = res.data.data
    const arr = res.data.data.location.split('/')
    this.selectedOptions = [TextToCode[arr[0]].code, TextToCode[arr[0]][arr[1]].code, TextToCode[arr[0]][arr[1]][arr[2]].code]

    // console.log(JSON.parse(localStorage.getItem('CKID')))
  },
  methods: {
    // 地区选择器改变
    changeFn(val) {
      // console.log(val)
      // this.nodeForm.areaCode = val[3]
      // this.addr = `${CodeToText[val[0]]}-${CodeToText[val[1]]}-${CodeToText[val[2]]}`
      // console.log(this.addr)
      this.formData.location = `${CodeToText[val[0]]}/${CodeToText[val[1]]}/${CodeToText[val[2]]}`
    },
    async submit() {
      await aupdataCK({
        ...this.formData,
        province: this.selectedOptions[0],
        area: this.selectedOptions[1],
        city: this.selectedOptions[2]
      })
      this.$message.success('新增成功')
      this.$router.go(-1)
    }
  }
}

</script>
<style lang='scss' scoped>
.el-card {
  // display: flex;
  margin: 16px;
  border-radius: 23px;
}
.el-form--inline .el-form-item {
    // display: inline-block;
    margin-right: 36px;
    // vertical-align: top;
}
// ::v-deep .el-input__inner {
//   border-radius: 11px;
//   // margin-right: 30px;

// }
::v-deep .el-form-item__label {
  color: #999
}
.addFooter {
  margin-left: 300px;
  margin-top: 15px;
}
.el-button {
  width: 190px;
  margin-right: 50px;
  color: black;
}
</style>
